package edu.mobbuzz.page.menu;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import edu.mobbuzz.page.menu.category.CategoryPage;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.theme.Theme;
import java.io.IOException;

public class CategoryMenu extends Menu{
    private Form categoryForm;
    private Container categoryPanel; 
    
    private Label menuTitle;
    private Label menuIconLabel;
    private Label menuDescription;
    private Image menuIcon;   
    
    public CategoryMenu(){
        categoryForm = new Form(){
            public void keyPressed(int keyCode) {
                gameActionPerformed(keyCode);
            }        
        };             
        categoryPanel = new Container();
        
        menuTitle = new Label(getMenuTitle());
        menuDescription = new Label();
        try {
            menuIcon = Image.createImage("/icons/category.gif"); 
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        menuIconLabel = new Label(menuIcon);  
    }
    public void showPage(){ 
        menuIconLabel.setAlignment(Component.CENTER);
        menuIconLabel.getStyle().setMargin(50, 0, 0, 0);
        menuIconLabel.getStyle().setBgTransparency(0);
        
        menuTitle.setAlignment(Component.LEFT);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setFgColor(0xF59FBC);
        menuTitle.getStyle().setBgTransparency(0);
        
        menuDescription.setText("Create category based on your RSS");      
        menuDescription.setAlignment(Component.LEFT);
        menuDescription.getStyle().setFont(Theme.getFontDescriptionStyle("SansSerif"));
        menuDescription.getStyle().setFgColor(0xFFFFFF);
        menuDescription.getStyle().setMargin(0, 0, 5, 0);         
        menuDescription.getStyle().setBgTransparency(0);
        
           
        categoryPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));        
        categoryPanel.addComponent(menuIconLabel);
        categoryPanel.addComponent(menuTitle);
        categoryPanel.addComponent(menuDescription);
        
        categoryForm.setLayout(new BorderLayout());     
        categoryForm.addComponent(BorderLayout.CENTER, categoryPanel);        
        categoryForm.addCommand(getExitCmd());
        categoryForm.addCommand(getOnlineCmd());
        categoryForm.addCommand(getSignOutCmd());
        categoryForm.addCommand(new Command("Open") {
            public void actionPerformed(ActionEvent evt) {
               openCategory();
            }
        });
            
        categoryForm.setCommandListener(getActionlistener());
        categoryForm.show();
    }
    
    public String getMenuTitle() {
        return MenuString.CATEGORY;
    }
    
    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);
        if(game == Display.GAME_FIRE){
            openCategory();
        }else if(game == Display.GAME_RIGHT){
            getCategoryTransitionRight();
        }else if(game == Display.GAME_LEFT){
            getCategoryTransitionLeft();
        }
    }
    
    public void getCategoryTransitionRight(){
        categoryForm.setTransitionOutAnimator(Theme.getTransitionOut());
        RSSChannelMenu rssChannelMenu = new RSSChannelMenu();
        rssChannelMenu.setExitCmd(getExitCmd());
        rssChannelMenu.setActionlistener(getActionlistener());
        rssChannelMenu.setMessaging(getMessaging());
        rssChannelMenu.showPage();
    }
    
    public void getCategoryTransitionLeft(){
       categoryForm.setTransitionOutAnimator(Theme.getTransitionIn());
       OutboxMenu outboxMenu = new OutboxMenu();
       outboxMenu.setExitCmd(getExitCmd());
       outboxMenu.setActionlistener(getActionlistener());
       outboxMenu.setMessaging(getMessaging());
       outboxMenu.showPage();
    }
    
    public void openCategory() {
        CategoryPage categoryPage = new CategoryPage();
        categoryPage.setExitCmd(getExitCmd());
        categoryPage.setActionlistener(getActionlistener());
        categoryPage.setMessaging(getMessaging());
        categoryPage.showPage();
    }
}
